package settings;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;

import dataStorage.DataHandler;
import dataStorage.DataXML;

// this class is a singleton
public class IdManager {
	private static IdManager instance = null;
	private static String key = "idObject";
	public ArrayList<IdObject> allId;

	protected IdManager() {

	}

	public static IdManager getInstance() {
		if (instance == null) {
			instance = new IdManager();
		}
		return instance;
	}

	public boolean isPresent(String rootKey)
			throws TransformerConfigurationException, InstantiationException,
			IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		loadAll();
		for (int i = 0; i < allId.size(); i++) {
			IdObject id = (IdObject) allId.get(i);
			if (id.getId().equals(rootKey)) {
				return true;
			}
		}
		System.out.println("FALSE");
		return false;
	}

	public String getLastId(String rootKey)
			throws TransformerConfigurationException, InstantiationException,
			IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		DataHandler dataHandler = DataXML.getInstance();
		loadAll();
		//System.out.println("ID SIZE:: " + allId.size());
		for (int i = 0; i < allId.size(); i++) {
			IdObject id = (IdObject) allId.get(i);
			if (id.getId().equals(rootKey)) {
				System.out.println("ID GET:: " + id.getId() + id.getIdvalue());
				return id.getId() + id.getIdvalue();
			}
		}
		return "";
	}

	public void loadAll() throws TransformerConfigurationException,
			InstantiationException, IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		allId = new ArrayList<IdObject>();
		DataHandler dataHandler = DataXML.getInstance();
		allId = dataHandler.load(key, IdObject.class);
	}

	public boolean updateLastId(String rootKey)
			throws TransformerConfigurationException, InstantiationException,
			IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		/*loadAll();
		DataHandler dataHandler = DataXML.getInstance();
		//Finds the correct entry e.g "Goal" and increments it
		for (int i = 0; i < allId.size(); i++) {
			IdObject id = (IdObject) allId.get(i);
			//System.out.println("00---"+id.getId());
			if (id.getId().equals(rootKey)) {
				id.setIdvalue(id.getIdvalue() + 1);
				dataHandler.update(rootKey, id, key, IdObject.class);
				return true;
			}
		}
		//if can't find ID then create a new key
		if (addLastId(rootKey)) {
			//and try again
			for (int i = 0; i < allId.size(); i++) {
				IdObject id = (IdObject) allId.get(i);
				//System.out.println("00---"+id.getId());
				if (id.getId().equals(rootKey)) {
					id.setIdvalue(id.getIdvalue() + 1);
					dataHandler.update(rootKey, id, key, IdObject.class);
					return true;
				}	
			}
		}//otherwise there must be something seriously worng
		else System.out.println("IdManager could not find the correct key, and could not create it");
		return false;*/
		loadAll();
		DataHandler dataHandler = DataXML.getInstance();
		for (int i = 0; i < allId.size(); i++) {
			IdObject id = (IdObject) allId.get(i);
			System.out.println("00---"+id.getId());
			if (id.getId().equals(rootKey)) {
				id.setIdvalue(id.getIdvalue() + 1);
				dataHandler.update(rootKey, id, key, IdObject.class);
				return true;
			}
		}
		return false;
	}

	public boolean addLastId(String rootKey)
			throws TransformerConfigurationException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, IllegalAccessException, InstantiationException {
		IdObject id = new IdObject();
		id.setIdvalue(0);
		id.setId(rootKey);

		DataHandler dataHandler = DataXML.getInstance();
		dataHandler.write(id, key, IdObject.class);
		System.out.println("ID WRITE");
		return true;
	}
}
